// pages/active/index.js
var app = getApp();
var core = app.requirejs('core');
var diypage = app.requirejs('biz/diypage');
var diyad = app.requirejs('biz/ad');
const api = require("../../utils/api.js")
const authScope = require("../../utils/auth.js")

Page(app.sr.page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [{
      select: false,
      pageurl: "/pages/custom/index?pageid=103407"
    }, {
      select: false,
      pageurl: "/pages/custom/index?pageid=103409"
    }, {
      select: false,
      pageurl: "/pages/custom/index?pageid=103410"
    }, {
      select: false,
      pageurl: "/pages/custom/index?pageid=103411"
    }, {
      select: false,
      pageurl: "/pages/custom/index?pageid=103408"
    }],
    selnumber: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideTabBar({
      // animation: true,
    })
    this.selMail()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let isLogin = app.checkAuth();
    this.setData({
      isLogin
    })
    this.judgeOrder()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh({
      complete: (res) => {},
      fail: (res) => {},
      success: (res) => {},
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var url = null;
    var title = null;
    if (this.data.diytitle) {
      url = '/pages/index/index'
      title = this.data.diytitle
    }
    return core.onShareAppMessage(url, title);
  },
  onShareTimeline: function (res) {
    return {
      title: this.data.diytitle,
      query: ''
    }
},
  selectgood(e) {
    console.log(e)
    let index = e.currentTarget.dataset.index
    let list = this.data.list
    let selnumber = this.data.selnumber
    let value = list[index].select

    if (value == false) {
      if (selnumber >= 2) {
        wx.showToast({
          title: '5种口味任选2包',
          icon: "none"
        })
        return
      }
      list[index].select = true
      selnumber++
    } else {
      list[index].select = false
      selnumber--
    }
    this.setData({
      selnumber,
      list

    })
  },
  //跳转
  jump(e) {
    let url = e.currentTarget.dataset.url
    wx.navigateTo({
      url,
      complete: (res) => {},
      fail: (res) => {},
      success: (result) => {},
    })
  },
  nowbuy() {
    if (this.data.judgeOrder) {
      wx.showToast({
        title: '您已经申请一次了',
        icon: "none"
      })
      return false
    }
    let selnumber = this.data.selnumber
    if (selnumber == 0) {
      wx.showToast({
        title: '您尚未选择商品，请先选择',
        icon: "none"
      })
      return false
    }
    if (selnumber == 1) {
      wx.showToast({
        title: '需要选择2件商品后才可结算',
        icon: "none"
      })
      return false
    }
    let list = this.data.list
    let goodsIds = []
    list.forEach(e => {
      if (e.select == true) {
        goodsIds.push(e.id)
      }
    })
    goodsIds = goodsIds.join(",")
    let url = '/pages/order/create/index?ordertype=1&goodsIds=' + goodsIds + "&type=1&total=1"
    wx.navigateTo({
      url,
      complete: (res) => {},
      fail: (res) => {},
      success: (result) => {},
    })
  },
  nowbuy1(e) {
    let $this = this
    let id = e.currentTarget.dataset.id
    let url = '/pages/order/create/index?ordertype=1&goodsIds=' + id + "&type=1&total=1"
    wx.showLoading({
      title: '加载中',
    })
    let apicontent = api.orders.selectMemberQualifications()
    core.get(apicontent, {
      goodsId: id
    }, function (res) {

      wx.hideLoading({
        complete: (res) => {},
      })
      if (res.code == 1) {
        if (!res.data) {
          wx.showToast({
            title: '您已经申请一次了',
            icon: "none"
          })
          return false
        }
        wx.navigateTo({
          url,
          complete: (res) => {},
          fail: (res) => {},
          success: (result) => {},
        })
      } else {
        wx.showToast({
          title: res.message,
          icon: "none"
        })
      }

    })

  },
  //查询付邮商品
  selMail() {
    let $this = this
    let list = this.data.list
    let apicontent = api.goods.selMail()
    core.get(apicontent, {
      // ...data
    }, function (res) {
      console.log(res)
      let goodlist = res.data
      // for (let i = 0; i < 4; i++) {
      //   list.push(ele)
      //     list[0].select = false
      //     list[0].pageurl = "/pages/custom/index?pageid=103407"
      // }
      goodlist.forEach(ele => {
        if (ele.title.includes("宫保鸡丁")) {
          list[0].id = ele.id
        }
        if (ele.title.includes("黑椒牛柳")) {
          list[1].id = ele.id
        }
        if (ele.title.includes("鱼香肉丝")) {
          list[2].id = ele.id
        }
        if (ele.title.includes("糖醋排骨")) {
          list[3].id = ele.id
        }
        if (ele.title.includes("麻婆豆腐")) {
          list[4].id = ele.id
        }
      })
      $this.setData({
        goodlist,
        list
      })
    })
  },
  //查询付邮商品
  judgeOrder() {
    let $this = this
    let apicontent = api.orders.judgeOrder()
    core.get(apicontent, {

    }, function (res) {
      console.log(res)
      $this.setData({
        judgeOrder: res.data
      })
    })

  },
  //获取登录授权
  bindgetuserinfo() {
    let $this = this;
    authScope.scopeLogin().then((res) => {
      if (res) {
        $this.onShow();
      }
    })
  },
}))